<template>
  <el-row >
    <el-col :span="leftColSpan" style="margin-bottom: 20px">
      <div class="app-container">
        <!-- 查询区域 -->
        <el-form :model="queryParams" :ref="queryFormRef" inline v-show="showSearch" label-width="auto" @keyup.enter.native="handleQuery">
            <el-form-item label="关键字" prop="keywords" style="width: 220px;">
              <el-input v-model="queryParams.keywords" placeholder="请输入关键字" clearable style="width: 150px;"/>
            </el-form-item>
            <el-form-item label="状态" prop="status">
              <lam-dict-select v-model="queryParams.status" dictCode="status_type" placeholder="状态" clearable />
            </el-form-item>
            <el-form-item label-width="0px">
              <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
              <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>
        <!-- 查询区域-END -->

        <!-- 操作按钮区域 -->
        <div class="tool-bar-btn">
          <el-button type="primary" icon="el-icon-plus" @click="handleAdd()" v-hasPerms="['sys:dept:add']">新增</el-button>
          <el-button type="warning" icon="el-icon-download" @click="handleExport" v-hasPerms="['sys:dept:export']">导出</el-button>
          <el-button icon="el-icon-close" @click="closeUserHandler" v-if="rightColSpan"  class="close-user-bt" :style="closeUserStyle">关闭用户</el-button>
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </div>

        <!-- 表格区域 -->
        <el-table :ref="dataTableRef" v-loading="loading" :data="dataList" stripe border @selection-change="onSelectChange"
              :tree-props="{children: 'children', hasChildren: 'hasChildren'}" :row-key="rowKey">
          <el-table-column label="机构名称" align="center" prop="deptName"  width="200" fixed="left"/>
          <el-table-column label="机构编码" align="center" prop="deptCode"  width="180" />
          <el-table-column label="状态" align="center" prop="statusDictText" width="80">
            <template slot-scope="{row}"><LamStatus :value="row.status" :text="row.statusDictText"/></template>
          </el-table-column>
          <el-table-column label="创建人" align="center" prop="createBy"  width="120" />
          <el-table-column label="创建时间" align="center" prop="createTime" width="180"/>
          <el-table-column label="更新人" align="center" prop="updateBy"  width="120" />
          <el-table-column label="更新时间" align="center" prop="updateTime" width="180"/>
          <el-table-column label="备注" align="center" prop="remark" :show-overflow-tooltip="true" />
          <el-table-column label="操作" align="center" fixed="right" class-name="small-padding fixed-width" width="230">
            <template slot-scope="{row}">
              <el-button type="text" icon="el-icon-user" @click="handleUser(row)" v-hasPerms="['sys:user:toSelectList:deptUser']">用户</el-button>
              <el-button type="text" icon="el-icon-plus" @click="handleAdd(row.id)" v-hasPerms="['sys:dept:add']">添加下级</el-button>
              <el-dropdown>
                <el-link type="primary" icon="el-icon-arrow-down" :underline="false" class="el-button--small">更多</el-link>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item v-hasPerms="['sys:dept:edit']">
                    <el-button type="text" icon="el-icon-edit" @click="handleUpdate(row)">修改</el-button>
                  </el-dropdown-item>
                  <el-dropdown-item v-hasPerms="['sys:dept:remove']">
                     <el-button type="text" icon="el-icon-delete" @click="handleDelete(row)">删除</el-button>
                  </el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </template>
          </el-table-column>
        </el-table>

        <pagination v-show="!loading" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"  @pagination="getList" />

        <!-- 添加或修改对话框 -->
        <EditDept ref="formDialog" @ok="getList" />

      </div>
    </el-col>
      <el-col :span="rightColSpan">
        <!-- 组织机构用户列表 -->
        <DeptUserList  ref="deptUserList" :deptId="curRow.id" :deptName="curRow.deptName"/>
      </el-col>

    </el-row>
</template>

<script>
import {httpGet, httpDel} from '@/api';
import { IndexListMixin } from '@/components/mixins/IndexListMixin';
import EditDept from './edit';
import DeptUserList from './deptUserList';
import {hexToRGB} from '@/utils/cssUtil'

export default {
  name: "Dept",
  mixins:[IndexListMixin],
  components: {
    EditDept,
    DeptUserList
  },
  data() {
    return {
      // 查询参数
      queryParams: {
        keywords: null,
        deptCodeKey: null,
        deptNameKey: null,
        status: null,
        pageNum: 1,
        pageSize: 10
      },
      url: {
      	exportXls: '/sys/dept/export',
        base: '/sys/dept/',
      	list: '/sys/dept/list'
      },
      deptUserShow: false,
      curRow:{}
    };
  },
  created() {
    this.getList();
  },
  computed:{
    leftColSpan() {
      return this.deptUserShow ? 14 : 24
    },
    rightColSpan() {
      return this.deptUserShow ? 10 : 0
    },
    closeUserStyle(){
      return {
        'background-color': hexToRGB(this.$store.state.settings.theme, 30)
      }
    }
  },
  methods: {
    getList() {
      this.loadData(this.url.list);
    },

    /** 用户按钮操作 */
    handleUser(row) {
      this.curRow = row;
      this.deptUserShow = true;
    },
    /** 关闭用户操作 */
    closeUserHandler() {
      this.curRow = {};
      this.deptUserShow = false;
    },

    /** 新增按钮操作 */
    handleAdd(id) {
      this.$refs.formDialog.toAdd("添加组织机构", {parentId: id});
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      const id = row.id || this.selectedRowKeys
      this.$refs.formDialog.toUpdate("修改组织机构", httpGet(this.url.base + id));
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.selectedRowKeys
      this.deleteData(this.url.base, ids);
    },

    /** 导出按钮操作 */
    handleExport() {
      this.exportData(this.url.exportXls);
    }

  }
};
</script>

<style scoped>
  .close-user-bt{
    position: relative;
    float: right;
  }
</style>
